# 11 - Laravel 5.8 Nova
Nova (opens new window) 是由 Laravel 官方團隊開發的後台管理面板 https://nick-basile.com/blog/post/getting-started-with-laravel-nova
這裡使用了andersao/l5-repository (opens new window), 但不是Nova的重點!
透過artisan生成l5-repository Model
php artisan make:entity News
全部選y
一共生成了
app/Contracts/PostRepository.phpapp/Entities/Post.phpapp/Http/Controllers/API/PostsController.phpapp/Http/Controllers/WCMS/PostsController.phpapp/Http/Requests/PostCreateRequest.phpapp/Http/Requests/PostUpdateRequest.phpapp/Http/Resources/PostCollection.phpapp/Nova/Post.php- 主要是在這個file
app/Presenters/PostPresenter.phpapp/Providers/RepositoryServiceProvider.php- 在boot( )加了一行
app/Repositories/PostRepositoryEloquent.phpapp/Validators/PostValidator.phpdatabase/migrations/2019_08_19_180303_create_posts_table.php
app/Nova/Post.php
public function fields(Request $request)
{
return [
ID::make()->sortable(),
Text::make('Name', 'name')
->sortable()
->rules('required', 'max:255'),
BelongsTo::make('Category', 'productCategory', 'App\Nova\ProductCategory'),
//BelongsTo::make('Category', 'productCategory', ProductCategory::class),
Image::make('image')->disk('public'),
Boolean::make('Enabled'),
];
}
坑: 不能任何column名不可以有任何大寫!!! 會讀唔到,但又唔會出error... 例如
image唔可以imagePath


未完...
# Filter
app/Nova/Product
class ProductCategory extends Resource
{
/**
* The model the resource corresponds to.
*
* @var string
*/
public static $model = 'App\Entities\Product';
...
public function filters(Request $request)
{
return [
new Filters\ProductCategoryType
];
}
}
app/Nova/Filters/ProductCategoryType.php
class ProductCategoryType extends Filter
{
public function apply(Request $request, $query, $value)
{
return $query->where('categories_id', $value);
}
public function options(Request $request)
{
//https://stackoverflow.com/a/52632103/5588637
$categories = \App\Entities\ProductCategory::all();
return $categories->pluck('id', 'name')->all();
}
}

贊助商連結